<template>
  <div class="login">
    <image style="width: 100vw;height: 100vh;position: fixed;left: 0;top: 0;z-index: -1" src="/static/image/login_bg.png"></image>
    <Navbar isBack></Navbar>
    <div class="login-form">
      <image style="width: 240rpx;height: 240rpx" src="/static/image/logo.png"></image>
      <div class="login-button">
        <button class="primary-button" @click="onLogin">一键登录</button>
      </div>
    </div>
    <view class="login-footer">
      <view class="check" style="display: flex;align-items: center">
        <div style="display: flex;align-items: center" @click="setAgree()">
          <image v-if="agree" class="publish-icon" src="/static/radio_fill.png"></image>
          <image v-else class="publish-icon" src="/static/radio.png"></image>
          <text style="color: #8F959E;font-size: 28rpx">我已阅读并同意</text>
        </div>
        <text style="color: #E68627"  @click="setAgree2('privacy_policy')">《隐私政策》</text>
        <text style="color: #8F959E;font-size: 28rpx">和</text>
        <text style="color: #E68627"  @click="setAgree2('user_agreement')">《用户协议》</text>
      </view>
    </view>
  </div>
</template>
<script>
import Page from "../../components/Page.vue";
import Navbar from "../../components/Navbar.vue";

export default {
  name: "index",
  components: {Navbar, Page},
  data() {
    return {
      agree: false,
    }
  },
  onLoad() {
    this.$store.commit('LOGOUT')
  },
  methods: {
    onLogin() {
      if (!this.agree) {
        uni.showToast({
          title: '请阅读并同意《登录协议》',
          icon: 'none'
        })
        return
      }
      let that = this
      wx.login({
        success(loginRes) {
          that.$api.wxLogin({
              code: loginRes.code,
            }).then(response => {
              uni.hideLoading()
              uni.showToast({ title: '登录成功', icon: 'success' });
              setTimeout(() => {
                that.$store.commit('SET_USERINFO', {
                  ...response,
                  is_register: false
                })
                if (response.is_register) {
                  // 重定向完善个人信息
                  uni.redirectTo({
                    url: '/pages/mine/edit'
                  })
                } else {
                  uni.navigateBack()
                }
              }, 300);
          })
        }
      })
    },
    setAgree() {
      this.agree = !this.agree;
    },
    setAgree2(key) {
      uni.navigateTo({
        url: '/pages/login/service?key=' + key
      })
    },
  }

}
</script>

<style lang="scss">
.login {
  height: 100%;
  overflow: hidden;
  .login-form {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-top: 200rpx;
  }
  .login-button {
    padding: 60rpx 0 30rpx 0;
    display: flex;
    align-items: center;
    justify-content: center;

    .primary-button {
      width: 630rpx;
      background: rgba(105, 192, 110, 1);
    }
  }

  .login-footer {
    padding-bottom: 100rpx;
    width: 100%;
    font-size: 28rpx;
    font-weight: normal;
    color: #999;
    display: flex;
    justify-content: center;
    margin-top: 30rpx;

    .publish-icon {
      width: 32rpx;
      height: 32rpx;
      margin-right: 10rpx;
    }
  }
}
</style>
